package com.hydra.wiki.doc.controller;

import com.hydra.wiki.core.page.ViewMode;
import com.hydra.wiki.doc.domain.FarmDocfile;
import com.hydra.wiki.doc.domain.ex.TypeBrief;
import com.hydra.wiki.doc.server.FarmDocTypeInter;
import com.hydra.wiki.doc.server.FarmFileManagerInter;
import com.hydra.wiki.doc.server.FarmFileManagerInter.FILE_TYPE;
import com.hydra.wiki.doc.util.ExcelUtils;
import com.hydra.wiki.web.WebUtils;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * 文档管理
 *
 */
@RequestMapping("/actionImg")
@Controller
public class ActionImgQuery extends WebUtils {
    private static final Logger log = Logger.getLogger(ActionImgQuery.class);

    @Resource
    private FarmFileManagerInter farmFileManagerImpl;
    @Resource
    private FarmDocTypeInter farmDocTypeManagerImpl;

    /**
     * 上传附件文件
     *
     * @return
     */
    @RequestMapping(value = "/PubFPupload.do")
    @ResponseBody
    public Map<String, Object> upload(@RequestParam(value = "imgFile", required = false) MultipartFile file,
                                      HttpServletRequest request, ModelMap model, HttpSession session) {
        int error;
        String message;
        String url = null;
        String id = null;
        String fileName = "";
        try {
            String fileid = null;
            // 验证大小
            /*String maxLength = FarmParameterService.getInstance().getParameter("config.doc.upload.length.max");
            if (file.getSize() > Integer.valueOf(maxLength)) {
                throw new Exception("文件不能超过" + Integer.valueOf(maxLength) / 1024 + "kb");
            }*/
            CommonsMultipartFile cmFile = (CommonsMultipartFile) file;
            DiskFileItem item = (DiskFileItem) cmFile.getFileItem();
            {// 小于8k不生成到临时文件，临时解决办法。
                if (!item.getStoreLocation().exists()) {
                    item.write(item.getStoreLocation());
                }
            }

            fileName = URLEncoder.encode(item.getName(), "utf-8");
            // 验证类型
            /*List<String> types = parseIds(FarmParameterService.getInstance().getParameter("config.doc.upload.types")
                    .toUpperCase().replaceAll("，", ","));
            if (!types.contains(file.getOriginalFilename()
                    .substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length())
                    .toUpperCase())) {
                throw new Exception("文件类型错误，允许的类型为：" + FarmParameterService.getInstance()
                        .getParameter("config.doc.upload.types").toUpperCase().replaceAll("，", ","));
            }*/
            fileid = farmFileManagerImpl.saveFile(item.getStoreLocation(), FILE_TYPE.HTML_INNER_IMG,
                    file.getOriginalFilename(), getCurrentUser(session));
            error = 0;
            url = farmFileManagerImpl.getFileURL(fileid);
            message = "";
            id = fileid;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
            error = 1;
            message = e.getMessage();
        }
        return ViewMode.getInstance().putAttr("error", error).putAttr("url", url).putAttr("message", message)
                .putAttr("id", id).putAttr("fileName", fileName).returnObjMode();
    }

    /**
     * 上传图片文件
     *
     * @return
     */
    @RequestMapping(value = "/PubFPuploadImg.do")
    @ResponseBody
    public Map<String, Object> PubFPuploadImg(@RequestParam(value = "imgFile", required = false) MultipartFile file,
                                              HttpServletRequest request, ModelMap model, HttpSession session) {
        int error;
        String message;
        String url = null;
        String id = null;
        String fileName = "";
        try {
            String fileid = null;
            // 验证大小
            /*String maxLength = FarmParameterService.getInstance().getParameter("config.doc.upload.length.max");
            if (file.getSize() > Integer.valueOf(maxLength)) {
                throw new Exception("文件不能超过" + Integer.valueOf(maxLength) / 1024 + "kb");
            }*/
            CommonsMultipartFile cmFile = (CommonsMultipartFile) file;
            DiskFileItem item = (DiskFileItem) cmFile.getFileItem();
            {// 小于8k不生成到临时文件，临时解决办法。
                if (!item.getStoreLocation().exists()) {
                    item.write(item.getStoreLocation());
                }
            }

            fileName = URLEncoder.encode(item.getName(), "utf-8");
            // 验证类型
            /*List<String> types = parseIds(FarmParameterService.getInstance().getParameter("config.doc.img.upload.types")
                    .toUpperCase().replaceAll("，", ","));
            if (!types.contains(file.getOriginalFilename()
                    .substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length())
                    .toUpperCase())) {
                throw new Exception("文件类型错误，允许的类型为：" + FarmParameterService.getInstance()
                        .getParameter("config.doc.img.upload.types").toUpperCase().replaceAll("，", ","));
            }*/
            fileid = farmFileManagerImpl.saveFile(item.getStoreLocation(), FILE_TYPE.HTML_INNER_IMG,
                    file.getOriginalFilename(), getCurrentUser(session));
            error = 0;
            url = "/" + farmFileManagerImpl.getFileURL(fileid);
            message = "";
            id = fileid;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e);
            error = 1;
            message = e.getMessage();
        }
        return ViewMode.getInstance().putAttr("error", error).putAttr("url", url).putAttr("message", message)
                .putAttr("id", id).putAttr("fileName", fileName).returnObjMode();
    }

    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping("/Publoadfile")
    public void download(String id, HttpServletRequest request, HttpServletResponse response) {
        FarmDocfile file = null;
        try {
            file = farmFileManagerImpl.getFile(id);
        } catch (Exception e) {
            file = null;
        }
        if (file == null || file.getFile() == null || !file.getFile().exists()) {
            file = new FarmDocfile();
            file.setClassPathResource(farmFileManagerImpl.getNoneImgResource());
            file.setName("none");
        }
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        try {
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + new String(file.getName().getBytes("gbk"), "iso-8859-1"));
        } catch (UnsupportedEncodingException e2) {
            e2.printStackTrace();
        }

        InputStream is = null;
        OutputStream os = null;
        try {
            if(file.getFile() != null){
                is = new FileInputStream(file.getFile());
            }else {
                is = file.getClassPathResource().getInputStream();
            }
            os = response.getOutputStream();
            byte[] b = new byte[2048];
            int length;
            while ((length = is.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (FileNotFoundException e) {
            InputStream is1 = null;
            OutputStream os1 = null;
            try {
                ClassPathResource classPathResource = new ClassPathResource("META-INF/resources/text/img/none.png");
                is1 = classPathResource.getInputStream();
                os1 = response.getOutputStream();
                byte[] b = new byte[2048];
                int length;
                while ((length = is1.read(b)) > 0) {
                    os1.write(b, 0, length);
                }

            } catch (Exception e1) {
                log.error(e.getMessage());
            } finally {
                try {
                    is1.close();
                    os1.close();
                } catch (IOException e1) {
                    log.error(e.getMessage());
                }
            }
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            // 这里主要关闭。
            try {
                is.close();
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.error(e.getMessage());
            }
        }
    }
}
